/*
 * @lc app=leetcode.cn id=125 lang=csharp
 *
 * [125] 验证回文串
 */

// @lc code=start
public class Solution {
    int delta = 'a' - 'A';
    public bool IsPalindrome(string s) {
        int left = 0;
        int right = s.Length - 1;
        while (left < right)
        {
            while (left < right && !IsAlphanumeric(s[left]))
            {
                left++;
            }
            while (left < right && !IsAlphanumeric(s[right]))
            {
                right--;
            }
            if (left < right)
            {
                if (ToLower(s[left]) != ToLower(s[right]))
                {
                    return false;
                }
                left++;
                right--;
            }
        }
        return true;
    }
    
    private bool IsAlphanumeric(char c)
    {
        return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }
    
    private char ToLower(char c)
    {
        if (c >= 'A' && c <= 'Z')
        {
            return (char)(c + delta);
        }
        return c;
    }
    
}
// @lc code=end

